package com.awise.app.more.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.TextView;

import com.awise.R;
import com.awise.Widget.IndicatorSeekBar;
import com.awise.base.BaseActivity;
import com.awise.http.RoverGlobal;
import com.awise.util.Constant;
import com.awise.view.CircleProgress;

import java.util.Random;


/**
 * Created by lgz on 2018/1/23.
 */

public class TempActivity extends BaseActivity implements View.OnClickListener {

    private CircleProgress mCircleProgress;
    private IndicatorSeekBar isb_seekbar;
    private Random mRandom;
    private int progressValue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.more_temp_layout);
        initView();
        LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, new IntentFilter(Constant.TEMPERATURE_ACTION));
        readTemperatureData();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver);
    }

    private void initView() {
        TextView s_back = (TextView) this.findViewById(R.id.tv_back);
        s_back.setVisibility(View.VISIBLE);
        s_back.setOnClickListener(this);
        TextView textView_content = (TextView) this.findViewById(R.id.textView_content);
        textView_content.setText(R.string.temperature_function);
        mCircleProgress = (CircleProgress) findViewById(R.id.circle_progress_bar1);
        isb_seekbar = (IndicatorSeekBar) findViewById(R.id.isb_seekbar);
        mCircleProgress.setOnClickListener(this);
//        mRandom = new Random();
        isb_seekbar.setOnSeekChangeListener(new IndicatorSeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(IndicatorSeekBar seekBar, int progress, float progressFloat, boolean fromUserTouch) {
//                System.out.println("IndicatorSeekBar:"+progress);
                progressValue=progress+30;
//                setdata((byte)progressValue);

            }

            @Override
            public void onSectionChanged(IndicatorSeekBar seekBar, int thumbPosOnTick, String textBelowTick, boolean fromUserTouch) {

            }

            @Override
            public void onStartTrackingTouch(IndicatorSeekBar seekBar, int thumbPosOnTick) {

            }

            @Override
            public void onStopTrackingTouch(IndicatorSeekBar seekBar) {
                setdata((byte)progressValue);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_back:
                finish();
                break;
            case R.id.circle_progress_bar1:
//                mCircleProgress.setValue(mRandom.nextInt((int) mCircleProgress.getMaxValue()));
                break;
        }
    }

    public void setdata(byte value){
     /*  设置温度值 c0 0a 11 01 22 00 00 01 1d 1c*/
//        0xC0 0x0A	0x11	0x01	0x22	35	0x00 0x01
        byte[] bytes = {(byte) 0xc0, 0x0a, 0x11, 0x01, 0x22, 0x00, 0x00, 0x01, value, 0x00};
        bytes[bytes.length-1] = RoverGlobal.getInstance().getAddValue(bytes, bytes.length);
        RoverGlobal.getInstance().connect.write(bytes);
    }

    public void readTemperatureData() {
        /* 读取温度值 c0 0a 11 01 1a 21 00 00 17*/
        byte[] bytes = {(byte) 0xc0, 0x0a, 0x11, 0x01, 0x1a, 0x21, 0x00, 0x00, 0x00};
        bytes[bytes.length - 1] = RoverGlobal.getInstance().getAddValue(bytes, bytes.length);
        RoverGlobal.getInstance().connect.write(bytes);
    }


    StringBuffer  buffers;
    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction() == Constant.TEMPERATURE_ACTION) {
                byte[] buffer = intent.getByteArrayExtra("socket_data");
                if (buffer != null) {
                    byte buffer1 = buffer[4];//0x1A读取数据
                    byte buffer2 = buffer[5];//0x21（温度分类）
                    byte buffer3 = buffer[8];//当前温度
                    byte buffer4 = buffer[9];//报警器温度
                    switch (buffer1) {
                        case 0x1a:
                            if (buffer2==0x21){
                                if (buffer3<=0)
                                    buffer3=20;
                                mCircleProgress.setValue(buffer3);
                                isb_seekbar.setProgress(buffer4-30);
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    };

}
